package com.keldian.mobilesafe.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;

import com.keldian.mobilesafe.R;
import com.keldian.mobilesafe.service.AddressService;
import com.keldian.mobilesafe.service.CallSmsSafeService;
import com.keldian.mobilesafe.service.FloatWindowService;
import com.keldian.mobilesafe.service.WatchDogService;
import com.keldian.mobilesafe.ui.SettingClickView;
import com.keldian.mobilesafe.ui.SettingItemView;
import com.keldian.mobilesafe.utils.ServiceUtils;
import com.keldian.mobilesafe.utils.SharePrefenceTool;

/**
 * Created by Administrator on 15-12-9.
 */
public class SettingActivity extends Activity {

    private SettingItemView settingItemView;
    private String versionName;

    // 设置是否开启显示归属地
    private SettingItemView siv_show_address;
    private Intent showAddress;

    private Intent showFloatWindow;

    //设置归属地显示框背景
    private SettingClickView scv_changebg;

    private SettingClickView scv_changeCallLocation;

    //设置黑名单开启关闭
    private SettingItemView siv_callsms_safe;
    private Intent callSmsSafeIntent;
    private SharedPreferences sp;
    private SettingItemView siv_watchdog;
    private Intent watchDogIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        initView();
        initAddressLocation();
    }

    /**
     * 初始化归属地位置
     */
    private void initAddressLocation() {
        scv_changeCallLocation = (SettingClickView) findViewById(R.id.scv_changeCallLocation);
        scv_changeCallLocation.setTitle("归属地提示框位置");
        scv_changeCallLocation.setDesc("设置归属地提示框位置");
        scv_changeCallLocation.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // 跳转到修改归属地位置的页面
                startActivity(new Intent(getApplicationContext(),
                        DragViewActivity.class));
            }
        });
    }

    private void initView() {
        sp = getSharedPreferences("mobilesafe", Context.MODE_PRIVATE);

        settingItemView = (SettingItemView) findViewById(R.id.siv_update);
        boolean update = SharePrefenceTool.getSharePreferences(SettingActivity.this);
        if (update) {
            settingItemView.setChecked(true);
//            settingItemView.setDesc("自动更新已经开启");
        } else {
            settingItemView.setChecked(false);
//            settingItemView.setDesc("自动更新已经关闭");
        }

        settingItemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //默认情况:自动更新已经关闭
                if (settingItemView.isChecked()) {
//                    setUpdateVersion(versionName);
                    settingItemView.setChecked(false);
//                    settingItemView.setDesc("自动更新已经关闭");
                    SharePrefenceTool.setSharePreferences(SettingActivity.this, false);
                } else {

                    settingItemView.setChecked(true);
//                    settingItemView.setDesc("自动更新已经开启");
                    SharePrefenceTool.setSharePreferences(SettingActivity.this, true);
                }
            }
        });
        //
        siv_show_address = (SettingItemView) findViewById(R.id.siv_show_address);
        showAddress = new Intent(this, AddressService.class);
        showFloatWindow = new Intent(this, FloatWindowService.class);

        boolean isServiceRunning = ServiceUtils.isServiceRunning(SettingActivity.this,
                "com.keldian.mobilesafe.service.AddressService");

        if (isServiceRunning) {
            //监听来电的服务是开启的
            siv_show_address.setChecked(true);
        } else {
            siv_show_address.setChecked(false);
        }

        siv_show_address.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (siv_show_address.isChecked()) {
                    // 变为非选中状态
                    siv_show_address.setChecked(false);
                    stopService(showAddress);
                    //移除小悬浮窗
                    stopService(showFloatWindow);

                } else {
                    // 选择状态
                    siv_show_address.setChecked(true);
                    startService(showAddress);
                    //显示小悬浮窗
                    startService(showFloatWindow);

                }
            }
        });

        //--------更换来电去电背景

        scv_changebg = (SettingClickView) findViewById(R.id.scv_changebg);
        scv_changebg.setTitle("归属地提示框风格");
        final String[] items = {"半透明", "活力橙", "卫士蓝", "金属灰", "苹果绿"};
        int which = SharePrefenceTool.getDialBackgroundIndex(this);
        scv_changebg.setDesc(items[which]);

        scv_changebg.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                int dd = SharePrefenceTool.getDialBackgroundIndex(SettingActivity.this);
                // 弹出一个对话框
                AlertDialog.Builder builder = new AlertDialog.Builder(SettingActivity.this);
                builder.setTitle("归属地提示框风格");
                builder.setSingleChoiceItems(items, dd, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        SharePrefenceTool.setDialBackgroundIndex(SettingActivity.this, which);
                        //保存选择参数
                        scv_changebg.setDesc(items[which]);

                        //取消对话框
                        dialog.dismiss();
                    }
                });
                builder.setNegativeButton("cancel", null);
                builder.show();
            }
        });


        //-------------------------来去电对话框位置----------------------











        //-------------------------------------------
        //黑名单


        siv_callsms_safe = (SettingItemView) findViewById(R.id.siv_callsms_safe);
        callSmsSafeIntent = new Intent(this, CallSmsSafeService.class);

        if (sp.getBoolean("blackList", false)) {
            siv_callsms_safe.setChecked(true);
        } else {
            siv_callsms_safe.setChecked(false);
        }

        siv_callsms_safe.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                SharedPreferences.Editor editor = sp.edit();
                if (siv_callsms_safe.isChecked()) {
                    // 变为非选中状态
                    editor.putBoolean("blackList", false);
                    siv_callsms_safe.setChecked(false);
                    stopService(callSmsSafeIntent);
                } else {
                    // 选择状态
                    editor.putBoolean("blackList", true);
                    siv_callsms_safe.setChecked(true);
                    startService(callSmsSafeIntent);
                }
                editor.commit();
            }
        });

        siv_watchdog = (SettingItemView) findViewById(R.id.siv_watchdog);
        watchDogIntent = new Intent(this, WatchDogService.class);

        if (sp.getBoolean("watchdog", false)) {
            siv_watchdog.setChecked(true);
        } else {
            siv_watchdog.setChecked(false);
        }

        siv_watchdog.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                SharedPreferences.Editor editor = sp.edit();
                if (siv_watchdog.isChecked()) {
                    // 变为非选中状态
                    editor.putBoolean("watchdog", false);
                    siv_watchdog.setChecked(false);
                    stopService(watchDogIntent);
                } else {
                    // 选择状态
                    editor.putBoolean("watchdog", true);
                    siv_watchdog.setChecked(true);
                    startService(watchDogIntent);
                }
                editor.commit();
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();

        showAddress = new Intent(this, AddressService.class);
        boolean isServiceRunning = ServiceUtils.isServiceRunning(
                SettingActivity.this,
                "com.keldian.mobilesafe.service.AddressService");

        if (isServiceRunning) {
            siv_show_address.setChecked(true);
        } else {
            siv_show_address.setChecked(false);
        }

        boolean isCallSMSeRunning = ServiceUtils.isServiceRunning(
                SettingActivity.this,
                "com.keldian.mobilesafe.service.CallSmsSafeService");

        siv_callsms_safe.setChecked(isCallSMSeRunning);

        boolean iswatchdogServiceRunning = ServiceUtils.isServiceRunning(
                SettingActivity.this,
                "com.keldian.mobilesafe.service.WatchDogService");

        siv_watchdog.setChecked(iswatchdogServiceRunning);


    }

    //当点击关闭自动更新时忽略本版本的升级，如果有更高版本，则仍提示升级
//    public void setUpdateVersion(String versionName){
//        this.versionName = versionName;
//        SharePrefenceTool.setUpdateVersion(this, versionName);
//    }


}
